package com.cheetah.service.impl;

import com.cheetah.dto.OrgDTO;
import com.cheetah.entity.Org;
import com.cheetah.entity.User;
import com.cheetah.exceptions.CheetahRuntimeException;
import com.cheetah.exceptions.UserError;
import com.cheetah.mapper.OrgMapper;
import com.cheetah.mapper.UserMapper;
import com.cheetah.service.OrgService;
import com.cheetah.utils.OrgUtil;
import com.cheetah.vo.OrgVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

import static com.cheetah.exceptions.UserError.ERROR_CODE_200206;

/**
 * @Author Administrator
 * @Date 2020/8/3
 * @Version V1.0
 * @Description:
 **/
@Service
public class OrgServiceImpl extends BaseServiceImpl implements OrgService {

    @Autowired
    private OrgMapper orgMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 保存部门
     * @param orgVO
     */
    @Override
    public OrgDTO save(OrgVO orgVO){

        Org org = orgMapper.selectByParentIdAndName(orgVO.getName().trim(),orgVO.getParent());

        Optional.ofNullable(org).ifPresent(o -> {
            throw new CheetahRuntimeException(UserError.ERROR_CODE_200201);
        });

        org = mapstructMapper.orgVO2org(orgVO);

        Org parent = orgMapper.selectByPrimaryKey(org.getParent());
        String longName = OrgUtil.contact(parent.getLongName(),org.getName());

        org.setLongName(longName);

        orgMapper.insert(org);
        String longId = OrgUtil.contact(parent.getLongId(),org.getId().toString());
        orgMapper.updateLongId(org.getId(),longId);
        org.setLongId(longId);
        return mapstructMapper.org2orgDto(org);
    }

    /**
     * 保存根部门
     * @param orgVO
     */
    @Override
    public OrgDTO saveRoot(OrgVO orgVO){

        Org org = mapstructMapper.orgVO2org(orgVO);
        org.setParent(0);
        org.setLongId(OrgUtil.contact("0"));
        org.setLongName(OrgUtil.contact(org.getName()));
        orgMapper.insert(org);

        return mapstructMapper.org2orgDto(org);
    }

    /**
     * 更新部门信息，不能修改部门长id
     * @param orgVO
     */
    @Override
    public void update(OrgVO orgVO){
        Org org = mapstructMapper.orgVO2org(orgVO);
        Org old = orgMapper.selectByPrimaryKey(orgVO.getId());

        if(StringUtils.equals(old.getLongId(),org.getLongId())){
            throw new CheetahRuntimeException(UserError.ERROR_CODE_200205);
        }

        Optional.ofNullable(old.getName()).ifPresent( n -> {
            if(!StringUtils.equals(n,org.getName())){
                //重命名部门名称，子部门需要更新部门长名称
                orgMapper.updateName(orgVO.getId(),orgVO.getName());
                orgMapper.updateSubOrgLongName(old.getLongName(),
                        OrgUtil.replace(old.getLongName(),
                        orgVO.getName()),old.getLongId());
            }else{
                orgMapper.updateByPrimaryKey(org);
            }
        });
    }

    /**
     * 移动部门
     * @param id 当前部门id
     * @param parentId 移动到的部门id下
     */
    @Override
    public void move(int id,int parentId){
        Org parent = orgMapper.selectByPrimaryKey(parentId);
        Org old = orgMapper.selectByPrimaryKey(id);
        orgMapper.updateParentId(id,parent.getId());
        orgMapper.updateSubOrgLongNameAndLongId(old.getLongName(),
                OrgUtil.contact(parent.getLongName(),old.getName()),
                old.getLongId(),parent.getLongId(),old.getLongId());
    }

    @Override
    public void delete(int id){
        User user = userMapper.selectOneByParentId(id);
        Optional.ofNullable(user).orElseThrow(() -> new CheetahRuntimeException(ERROR_CODE_200206));
    }
}
